﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

namespace Skupine_Web_Part.SkupineWP
{
    class SkupineEditor :EditorPart
    {
        TextBox username;
        TextBox password;
        TextBox ldap;
        TextBox dc;
        protected override void CreateChildControls()
        {
            username = new TextBox() { ID = "userName" };
            
            password = new TextBox() { ID = "pass" };
            password.TextMode = TextBoxMode.Password;            

            ldap = new TextBox() { ID = "ldapID" };
            dc = new TextBox() { ID = "dcID" };

            this.Controls.Add(new Label()
            {
                Text = @"<br/>Enter user name: domain\username<br/>",
                AssociatedControlID = username.ID
            });
            
            this.Controls.Add(username);
            
            this.Controls.Add(new Label()
            {
                Text = "<br/>Enter password:<br/>",
                AssociatedControlID = password.ID
            });
            
            this.Controls.Add(password);
            this.Controls.Add(new Label()
            {
                Text = "<br/>Enter domain information in format: DC=Corp,DC=Xnet,DC=si:<br/>",
                AssociatedControlID = ldap.ID
            });
            
            this.Controls.Add(ldap);
            this.Controls.Add(new Label()
            {
                Text = "<br/>Enter domain controler FQDN:<br/>",
                AssociatedControlID = dc.ID
            });
            this.Controls.Add(dc);
            this.Title = "Properties";

            base.CreateChildControls();
            this.ChildControlsCreated = true;
        }
        public override void SyncChanges()
        {
            EnsureChildControls();
            SkupineWP webPart = this.WebPartToEdit as SkupineWP;
            if (webPart != null)
            {
                username.Text = webPart.username;
                password.Text = webPart.password;
                ldap.Text = webPart.ldapQuery;
                dc.Text = webPart.DCName;
            }
        }
        public override bool ApplyChanges()
        {
            EnsureChildControls();
            SkupineWP webPart = this.WebPartToEdit as SkupineWP;
            if (webPart != null)
            {
                webPart.username = username.Text;
                webPart.password = password.Text;
                webPart.DCName = dc.Text;
                webPart.ldapQuery = ldap.Text;
            }
            return true;
        }
    }
}
